package biolean;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class CommandsListCanvas extends JPanel{
        private AppCanvas AppPanel;
        private ToolTipList CommandsList;
        
        public CommandsListCanvas(AppCanvas AppPanel) {
                this.AppPanel = AppPanel;
                        
                String[] data = Biolean.cmdRegister.getCommandsNames();
                CommandsList = new ToolTipList(data);
                CommandsList.setCellRenderer(new MyCellRenderer());
                this.add(CommandsList);
                CommandsList.addMouseListener(new MAdapter());
        }
        
        public JList getJlist() {
                return CommandsList;
        }       
        
        public AppCanvas getAppanel () {
                return AppPanel;
        }
        

        private class MAdapter extends MouseAdapter {

                public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 1) {
                 if (e.getClickCount() == 2) {
                     int index = CommandsList.getSelectedIndex();
                     Class instanceClass = Biolean.cmdRegister.getCommandClass(index);
                     String cmdName = Biolean.cmdRegister.getName(index);
                     AppPanel.addNewCommand(instanceClass, cmdName, null);
                  }
                }
                //AppPanel.requestFocus();
             }  
        }
        
        class MyCellRenderer extends JLabel implements ListCellRenderer, Commons {
             public Component getListCellRendererComponent(
               JList list,
               Object value,            // value to display
               int index,               // cell index
               boolean isSelected,      // is the cell selected
               boolean cellHasFocus)    // the list and the cell have the focus
             {
                 String s = value.toString();
                 setText(s);
                 /*if (s.equals("Filter")) {
                	 this.setToolTipText("som filter a toto je moj paradny description ");
                 } else
                 {
                	 this.setToolTipText("som vysek ludiaa");
                 }*/
                 this.setPreferredSize(new Dimension(COMMAND_LABEL_WIDTH,COMMAND_LABEL_HEIGHT));
                   if (isSelected) {
                     setBackground(list.getSelectionBackground());
                       setForeground(list.getSelectionForeground());
                   }
                 else {
                       setBackground(list.getBackground());
                       setForeground(list.getForeground());
                   }
                   setEnabled(list.isEnabled());
                   setFont(list.getFont());
               setOpaque(true);
               return this;
             }
         }

}